%{
#include "parser.tab.h"
#include <string.h>
#include "calc.h"

//#define YY_EXTRA_TYPE   parse_parm *
int lineno = 1;

#define PARM	yyget_extra(yyscanner)

#define YY_INPUT(buffer, res, max_size)				\
do {	                                                        \
	if (PARM->pos >= PARM->length)			       \
		res = YY_NULL;								\
	else											\
	{												\
		res = PARM->length - PARM->pos;				\
		res > (int)max_size ? res = max_size : 0;	\
		memcpy(buffer, PARM->buf + PARM->pos, res);	\
		PARM->pos += res;							\
	}												\
} while (0)

%}

%option reentrant bison-bridge
%option noyywrap
%option nounput

%%
SELECT		{ return SELECT; }
UPDATE		{ return UPDATE; }
INSERT		{ return INSERT; }
DELETE		{ return DELETE; }
CREATE		{ return CREATE; }
DROP		{ return DROP; }

AND         { return AND;  }
OR		    { return OR; }

VARCHAR		{ return VARCHAR; }
DOUBLE		{ return DOUBLE; }
INTEGER		{ return INTEGER; }

FROM		{ return FROM; }
INTO		{ return INTO; }
WHERE		{ return WHERE; }
SET			{ return SET; }
TABLE		{ return TABLE; }
VALUES		{ return VALUES; }

INFO      { return INFO; }
QUIT      { return QUIT; }
SAVE		{ return SAVE; }
OPEN		{ return OPEN; }
CREATEDB	{ return CREATEDB; }
DELETEDB	{ return DELETEDB; }
LOAD		{ return LOAD; }
PASSWD		{ return PASSWD; }
HELP		{ return HELP; }
REMOVEDB	{ return REMOVEDB; }
"="	|
"<"	|
">"	{ yylval->strval=strdup(yytext);return COMPARISON; }

[-+*/:(),.;]	{ yylval->strval=strdup(yytext); return yytext[0]; }


[A-Za-z][A-Za-z0-9_]*	{ yylval->strval=strdup(yytext); return NAME; }

[0-9]+	{ yylval->intval=atoi(yytext); return INTNUM;}
 
[0-9]+"."[0-9]* |
"."[0-9]*	{ yylval->floatval=atof(yytext); return APPROXNUM; }

[0-9]+[eE][+-]?[0-9]+	|
[0-9]+"."[0-9]*[eE][+-]?[0-9]+ |
"."[0-9]*[eE][+-]?[0-9]+	{ return APPROXNUM; }

	/* strings */

\"[^"'\n]*\"	{ yylval->strval=strdup(yytext); return STRING; }
		
'[^'\n]*$	{ return 0; }

\n	lineno++;

[ \t\r]+	;	/* white space */

"--".*$		;	/* comment */

%%

